package org.kl.bf.service.basic;

import java.util.Map;

import org.kl.bf.entity.basic.AttachmentMaster;
import org.kl.bf.repository.basic.AttachmentMasterDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Qken
 * @date 2014年3月28日
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class AttachmentMasterService {

	private AttachmentMasterDao attachmentMasterDao;

	@Autowired
	public void setAttachmentMasterDao(AttachmentMasterDao attachmentMasterDao) {
		this.attachmentMasterDao = attachmentMasterDao;
	}

	public AttachmentMaster getAttachmentMaster(String guid) {
		return attachmentMasterDao.findOne(guid);
	}

	public void saveAttachmentMaster(AttachmentMaster entity) {
		attachmentMasterDao.save(entity);
	}

	public void deleteAttachmentMaster(String guid) {
		attachmentMasterDao.delete(guid);
	}
	
	public Page<AttachmentMaster> getAttachmentMaster(Map<String, Object> searchParams, int pageNumber,
			int pageSize, String sortType, String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber,
				pageSize, sortType, orderType);
		Specification<AttachmentMaster> spec = buildSpecification(searchParams);
		return attachmentMasterDao.findAll(spec, pageRequest);
	}
	
	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<AttachmentMaster> buildSpecification(
			Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<AttachmentMaster> spec = KLDynamicSpecifications.byKLSearchFilter(
				filters.values(), AttachmentMaster.class);
		return spec;
	}
	
}
